package com.hardydou.system.service.impl;

import cn.hutool.core.date.DateUtil;
import com.hardydou.common.constant.Const;
import com.hardydou.common.result.Code;
import com.hardydou.common.result.ResultAssert;
import com.hardydou.framework.quartz.QuartzService;
import com.hardydou.system.mapper.SystemTaskMapper;
import com.hardydou.system.model.SystemTask;
import com.hardydou.system.service.ISystemTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：hardy
 * @description：系统任务服务
 * @date ：Created in 2020/6/25
 * @Copyright ：Hardydou.com
 */
@Service
public class SystemTaskServiceImpl implements ISystemTaskService {

    @Autowired
    SystemTaskMapper taskMapper;

    @Autowired
    QuartzService quartzService;

    @Override
    public void addTask(SystemTask task) {
        SystemTask tmp = findByGroupAndName(task.getTaskGroup(), task.getTaskName());
        ResultAssert.isNull(tmp, Code.TASK_IS_EXIST);
        Date now = DateUtil.date();
        task.setCtime(now);
        task.setUtime(now);
        taskMapper.insert(task);
        if (task.isOn()) {
            quartzService.addTask(task.toQuartzTask());
        }
    }

    @Override
    public void delTask(Long taskId) {
        SystemTask task = new SystemTask();
        task.setTaskId(taskId);
        task.setStatus(Const.Status.LOCKED.val);
        taskMapper.updateByPrimaryKeySelective(task);
        quartzService.deleteTask(task.toQuartzTask());
    }

    @Override
    public void update(SystemTask task) {
        taskMapper.updateByPrimaryKeySelective(task);
        quartzService.updateTask(task.toQuartzTask());
    }

    @Override
    public SystemTask findById(Long taskId) {
        return taskMapper.selectByPrimaryKey(taskId);
    }

    @Override
    public List<SystemTask> list(Map params) {
        return taskMapper.list(params);
    }

    @Override
    public SystemTask findByGroupAndName(String group, String name) {
        Map params = new HashMap();
        params.put("taskGroup", group);
        params.put("taskName", name);
        List<SystemTask> rs = taskMapper.list(params);
        if (rs.size() > 0) {
            return rs.get(0);
        }
        return null;
    }

    @Override
    public void startTask(SystemTask task) {
        ResultAssert.isFalse(task.isOn(), Code.TASK_IS_START);
        task.setStatus(Const.Status.NORMAL.val);
        taskMapper.updateByPrimaryKeySelective(task);
        quartzService.addTask(task.toQuartzTask());
    }

    @Override
    public void stopTask(SystemTask task) {
        ResultAssert.isTrue(task.isOn(), Code.TASK_IS_STOP);
        task.setStatus(Const.Status.LOCKED.val);
        taskMapper.updateByPrimaryKeySelective(task);
        quartzService.deleteTask(task.toQuartzTask());
    }


    /***
     * 重启时 加载job，<br/>
     * 这里无法通过 params.status=0 过滤数据，原因未知。
     */
    @PostConstruct
    public void init() {
        quartzService.clear();
        Map params = new HashMap();
        List<SystemTask> tasks = list(params);
        for (SystemTask task : tasks) {
            if (task.isOn()) {
                quartzService.addTask(task.toQuartzTask());
            }
        }
    }

}
